const express = require('express');
const router = express.Router();

const User = require('../controllers/UserController')
const fs = require('fs');
const dbConfig = require('../util/dbconfig');
// multer
const multer = require('multer');
const uuid = require('uuid')
const storage = multer.diskStorage({
  //保存路径
  destination: function (req, file, cb) {
    cb(null, './public/uploads')
    // 参数1是否转换二进制
    //注意这里的文件路径,不是相对路径，直接填写从项目根路径开始写就行了
  },
  //方案1:保存在destination的文件名.防止重名且保留文件原格式
  filename: function (req, file, cb) {
    let name = file.originalname
    let ext = name.substr(name.lastIndexOf('.'))
    cb(null, uuid.v4() + ext)
  }
})
const upload = multer({ storage: storage })

/* GET users listing. */
// 传参phone
router.post('/sendCode', User.sendCode);
// phone,code
router.post('/codePhoneLogin', User.codePhoneLogin)
// router.post('/sendCoreCode', User.sendCoreCode)
// username,password
router.post('/login', User.login)
// user_id, username
router.post('/editUserInfo', User.editUserInfo)
// user_id, oldpassword, newpassword
router.post('/setPassword', User.setPassword)
// user_id, email
router.post('/bindEmail', User.bindEmail)
router.post('/logout', User.logout)

// single('表单name值')// user_id,userpic
router.post('/editUserImg', upload.single('userpic'), User.editUserImg)
router.post('/uploadMoreImg', upload.array('flie', 9), User.uploadMoreImg)

router.post('/publish', User.publish)


module.exports = router;
